package com.cyf.plc.data.repository

import androidx.annotation.WorkerThread
import com.cyf.plc.data.local.dao.PlcDao
import com.cyf.plc.model.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject
import javax.inject.Singleton

@ExperimentalCoroutinesApi
@Singleton
class DatabaseRepository @Inject constructor(private val plcDao: PlcDao) {

    @WorkerThread
    fun getPlcAttributes(attributeIds: Array<String>): Flow<List<PlcAttribute>> =
        plcDao.getPlcAttributes(attributeIds).flowOn(Dispatchers.IO)

    @WorkerThread
    fun getPlcSubAttributes(attributeIds: Array<String>): Flow<List<PlcSubAttribute>> =
        plcDao.getPlcSubAttributes(attributeIds).flowOn(Dispatchers.IO)

    @WorkerThread
    fun getPlcAttributes(): Flow<MutableList<PlcAttribute>> =
        plcDao.getPlcAttributes().flowOn(Dispatchers.IO)

    @WorkerThread
    fun getPlcSubAttributes(): Flow<List<PlcSubAttribute>> =
        plcDao.getPlcSubAttributes().flowOn(Dispatchers.IO)

    @WorkerThread
    fun getFloorWithRooms(): Flow<List<FloorWithRooms>> =
        plcDao.getFloorWithRooms().flowOn(Dispatchers.IO)

    @WorkerThread
    fun saveFloors(plcFloors: List<PlcFloor>) = plcDao.insertFloors(plcFloors)

    @WorkerThread
    fun saveFloor(plcFloor: PlcFloor) = plcDao.insertFloor(plcFloor)

    @WorkerThread
    fun saveRooms(plcRooms: List<PlcRoom>) = plcDao.insertRooms(plcRooms)

    @WorkerThread
    fun saveRoom(plcRoom: PlcRoom) = plcDao.insertRoom(plcRoom)

    @WorkerThread
    fun savePlcAttributes(plcAttributes: List<PlcAttribute>) =
        plcDao.inertPlcAttributes(plcAttributes)

    @WorkerThread
    fun savePlcSubAttributes(plcSubAttributes: List<PlcSubAttribute>) =
        plcDao.inertPlcSubAttributes(plcSubAttributes)

    @WorkerThread
    fun savePlcSubAttribute(plcSubAttribute: PlcSubAttribute) =
        plcDao.inertPlcSubAttribute(plcSubAttribute)

    @WorkerThread
    fun savePlcAttribute(plcAttribute: PlcAttribute) = plcDao.inertPlcAttribute(plcAttribute)

    @WorkerThread
    fun deleteRooms(plcRooms: List<PlcRoom>) = plcDao.deleteRooms(plcRooms)

    @WorkerThread
    fun deleteRoom(plcRoom: PlcRoom) = plcDao.deleteRoom(plcRoom)

    @WorkerThread
    fun deletePlcFloor(plcFloor: PlcFloor) = plcDao.deletePlcFloor(plcFloor)

    @WorkerThread
    fun getAllPlcFaults(): Flow<List<PlcFault>> = plcDao.getFaults()

    @WorkerThread
    fun savePlcFaults(plcFaults: ArrayList<PlcFault>) = plcDao.savePlcFaults(plcFaults)

    @WorkerThread
    fun deleteAll() = plcDao.deleteAll()
}